<template>
	<view class="container">
		<view v-if="GoodList.length!=0">
			<view class="topSolt">
				<uni-row class="demo-uni-row" :width="720">
					<uni-col :span="8" v-for="(item,index) in soltTitle" :key="index">
						<view class="demo-uni-col dark" @tap="toggle(index)" :class="{color:toggleColor==index}">
							{{item}}
						</view>
					</uni-col>
				</uni-row>
			</view>

			<view style="margin-top:65rpx;">
				<view class="goods">
					<view class="goodsItem" v-for="(item,index) in GoodList" :key="index" @tap="gotoDetail(item.id)">
						<view>
							<image :src='item.pic' :lazy-load="true" webp="true"></image>
						</view>
						<view class="price">
							<text class="newPrice">￥{{item.price}}</text>
							<text class="olbPrice">￥{{item.originalPrice}}</text>
						</view>
						<view>
							<text class="productTitle">
								{{item.name}}
							</text>
						</view>
					</view>
				</view>
			</view>
			<ScrollTop :value="scrollTop" :ScrollTopTime="300"></ScrollTop>
		</view>

		<view v-if="showEmpty">
			<empty text="暂无数据"></empty>
		</view>


	</view>
</template>

<script>
	import ScrollTop from '@/components/ScrollTop.vue'
	export default {
		components: {
			ScrollTop
		},
		data() {
			return {
				params: {
					pageNum: 1,
					pageSize: 5,
					sort: "",
					categoryId: 0
				},
				GoodList: [],
				soltTitle: [
					"综合排序",
					"价格由低到高",
					"价格由高到低"
				],
				scrollTop: false,
				toggleColor: 0,
				showEmpty: false
			}
		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: options.cName
			})
			this.params.categoryId = options.cid
			this.getList()
			uni.showLoading({
				title: '加载中'
			});
		},
		methods: {
			getList() {
				setTimeout(() => {
					this.$api.category.productCatgory(this.params).then(res => {
						if (res.code == 200) {
							this.GoodList = res.list
							if (this.GoodList.length == 0) {
								this.showEmpty = true
							}
							uni.hideLoading();
						} else {
							uni.showToast({
								title: "请求失败",
								icon: "loading"
							})
						}
					})
				}, 800)
			},
			toggle(index) {
				this.toggleColor = index
				if (index == 0) {
					this.params.sort = ""
					this.getList()
				} else if (index == 1) {
					this.params.sort = "asc"
					this.getList()
				} else if (index == 2) {
					this.params.sort = "desc"
					this.getList()
				}
				uni.pageScrollTo({
					scrollTop: 0,
				})
			},
			onPageScroll(e) {
				if (e.scrollTop >= 500) {
					this.scrollTop = true
				} else {
					this.scrollTop = false
				}
			},
			gotoDetail(id) {
				uni.navigateTo({
					url: `/pages/view/ProductDetail?Pid=${id}`
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.topSolt {
			/* #ifdef H5 */
			position: fixed;
			top: 86rpx;
			z-index: 1000;
			/* #endif */

			/* #ifdef MP-WEIXIN */
			position: fixed;
			top: 0rpx;
			z-index: 1000;
			/* #endif */

			height: 60rpx;
			width: 100%;
			background-color: white;
			line-height: 60rpx;
			text-align: center;
			border-bottom: 1px solid gainsboro;

			.color {
				color: #cb0d0d;
				border-bottom: 2px solid #cb0d0d;
			}
		}

		.goods {
			background-color: #eeeeee;
			width: 100%;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.goodsItem {
				width: 355rpx;
				margin: 8rpx 8rpx;
				background-color: #ffffff;
				border-radius: 10rpx;
				overflow: hidden;
				box-shadow: 0px 0px 5px #e2e2e2;

				image {
					width: 100%;
					height: 200px;
				}

				.price {
					padding-left: 10rpx;

					.newPrice {
						font-size: 45rpx;
						color: #cb0d0d;
					}

					.olbPrice {
						color: gray;
						text-decoration: line-through;
					}

					text:nth-child(2) {
						margin-left: 17rpx;
					}
				}

				.shoppingsubTltie {
					padding-bottom: 30rpx;
				}

				.productTitle {
					color: gray;
				}
			}
		}

		.load-txt {
			padding: 20rpx 0;
			text-align: center;
			color: #cb0d0d;
			font-size: 24rpx;
		}
	}
</style>
